/**
 * Created by qunxing.du on 2016/7/28.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.zzcity.job;

import com.hw.one.collect.client.DataServiceFactory;
import com.hw.one.collect.zzcity.memento.ZzCityCareTaker;
import com.hw.one.collect.zzcity.memento.ZzCityMemento;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *  容错处理器
 */
@Component
public class ZzCityErrorHandlerJob extends ZzCityJob {
    private static Logger log = Logger.getLogger(ZzCityErrorHandlerJob.class);
    @Override
    protected void start() {
        Map<Integer, ZzCityMemento> mementos = ZzCityCareTaker.newInstance().getAll();
        Collection<ZzCityMemento> collection = mementos.values();
        log.info("郑州市备忘录数量："+ collection.size());
        for (Iterator<ZzCityMemento> i = collection.iterator(); i.hasNext(); ) {
            ZzCityMemento memento = i.next();
            String isSrc = memento.getIsSrc();
            String dataType = memento.getDataType();
            String startTime = memento.getStartTime();
            String endTime = memento.getEndTime();
            String uCode = memento.getuCode();
            int count = memento.getCount();
            String startDate = null;
            String endDate = null;
            try {
                 startDate =  URLDecoder.decode(startTime,"UTF-8");
                 endDate =  URLDecoder.decode(endTime,"UTF-8");
            } catch (UnsupportedEncodingException e) {
               log.error(e);
            }
            log.info("郑州市备忘录数据："+ isSrc +"|" + dataType+"|" + startDate +"|" + endDate +"|" + uCode+"|"+count);
            if(isAllowExecute(Integer.parseInt(isSrc),Integer.parseInt(dataType),endDate,count)){
                Map<String, String> map = new HashMap<>();
                map.put("startTime", startDate);
                map.put("endTime", endDate);
                map.put("isSrc", isSrc);
                map.put("dataType", dataType);
                map.put("uCode",uCode);
                execute(map);
            }
        }
    }

    /**
     * 是否已到允许执行的时间
     * @param isSrc
     * @param dataType
     * @param endTime
     * @return
     */
    private boolean isAllowExecute(int isSrc,int dataType,String endTime,int count){
        if(0 == count){
            return false;
        }
        int type = DataServiceFactory.newInstance().getType(isSrc, dataType);
        Date lastDate = null;//最近一次执行的结束日期
        try {
            lastDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime);
        } catch (ParseException e) {
            log.error(e);
        }
        Calendar c = Calendar.getInstance();
        if (type == 0 || type == 4) {
            c.add(Calendar.MINUTE,- RT_FAULT_TOLERANT_INTERVAL*count);
        }
        if (type == 1 || type == 5) {
            c.add(Calendar.MINUTE,- FIVEMIN_FAULT_TOLERANT_INTERVAL*count);
        }
        if (type == 2 || type == 7) {
            c.add(Calendar.MINUTE,- ONEHOUR_FAULT_TOLERANT_INTERVAL*count);
        }
        if (type == 3 || type == 8) {
            c.add(Calendar.MINUTE,- DAYAVG_FAULT_TOLERANT_INTERVAL*count);
        }
        Date intervalDate  = c.getTime();//间隔时间差

        return intervalDate.after(lastDate);
    }
}
